package tk.leafotto.sign2.activity;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;

import tk.leafotto.sign2.R;
import tk.leafotto.sign2.dao.DbUtils;
import tk.leafotto.sign2.dao.MySqliteOpenHelper;
import tk.leafotto.sign2.utils.TimeUtil;

public class SignActivity extends Activity {

    private DbUtils dbUtils;
    private String name;
    //签到数据
    private ArrayList<String> signList;
    private ListView lv_sign;
    private MyAdapter mAdapter;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(mAdapter == null){
                mAdapter = new MyAdapter();
                lv_sign.setAdapter(mAdapter);
            }else {
                mAdapter.notifyDataSetChanged();
            }
            tv_name.setText(name + "(" + signList.size() + ")");
        }
    };
    private TextView tv_name;
    private String date;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_sign);
        initUI();
        initData();

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(data == null){
            return;
        }
        date = data.getStringExtra("date");
        Date date = TimeUtil.getDate(this.date);
        //getDate是重载的方法，一个返回Date类型，一个返回String类型
        Date today = TimeUtil.getDate(TimeUtil.getDate());
        if (date.after(today)) {
            Toast.makeText(this,R.string.cannotsignfutrue,Toast.LENGTH_SHORT).show();
            return;
        }
        new Thread(){
            @Override
            public void run() {
                long result = dbUtils.afterSign(name, SignActivity.this.date);
                if(result > 0){
                    signList.add(SignActivity.this.date);
                    Collections.sort(signList,Collections.<String>reverseOrder());
                    mHandler.sendEmptyMessage(0);
                }
            }
        }.start();
    }

    private void initUI() {
        lv_sign = (ListView) findViewById(R.id.lv_sign);
        tv_name = (TextView) findViewById(R.id.tv_name);
        Button after_sign = (Button) findViewById(R.id.after_sign);
        after_sign.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), CalendarActivity.class);
                startActivityForResult(intent,1);
            }
        });
    }

    private void initData() {
        Intent intent = getIntent();
        name = intent.getStringExtra("name");

        MySqliteOpenHelper mOpenHelper = new MySqliteOpenHelper(this);
        dbUtils = DbUtils.instance(getApplicationContext());
        signList = new ArrayList<String>();
        new Thread(){
            @Override
            public void run() {
                if(dbUtils != null && name != null){
                    signList = dbUtils.getSign(name);
                    mHandler.sendEmptyMessage(0);
                }
            }
        }.start();
    }

    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return signList.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder = null;
            if (convertView == null) {
                convertView = convertView.inflate(getApplicationContext(),R.layout.item_lv_sign,null);
                viewHolder = new ViewHolder();
                viewHolder.tv_date = (TextView) convertView.findViewById(R.id.tv_date);
                viewHolder.bt_del = (Button) convertView.findViewById(R.id.bt_del);
                convertView.setTag(viewHolder);
            }else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            viewHolder.tv_date.setText(signList.get(position));

            viewHolder.bt_del.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new Thread(){
                        @Override
                        public void run() {
                            dbUtils = DbUtils.instance(getApplicationContext());
                            int result = dbUtils.delSign(name,signList.get(position));
                            signList.remove(signList.get(position));
                            if(result > 0){
                                mHandler.sendEmptyMessage(0);
                            }
                        }
                    }.start();
                }
            });
            return convertView;
        }
    }

    static class ViewHolder{
        TextView tv_date;
        Button bt_del;
    }
}